package com.feige.base.dto;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.feige.base.po.BasePo;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

/**
 * @author feige
 */
@Data
public class QueryBaseDto<T extends BasePo> implements BaseDto<T> {
    private static final long serialVersionUID = -4200452664177882007L;

    @JsonFormat(shape = JsonFormat.Shape.STRING)
    @ApiModelProperty(value = "id")
    private Long id;

    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "开始创建时间")
    private Date gmtCreatedBegin;

    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "结束创建时间")
    private Date gmtCreatedEnd;

    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "开始更新时间")
    private Date gmtModifiedBegin;

    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "结束更新时间")
    private Date gmtModifiedEnd;


    @Override
    public LambdaQueryWrapper<T> baseQueryWrapper(){
        LambdaQueryWrapper<T> queryWrapper = new LambdaQueryWrapper<>();
        Long id = this.getId();
        if (id != null){
            queryWrapper.ge(BasePo::getId, id);
        }
        Date gmtCreatedBegin = this.getGmtCreatedBegin();
        if (gmtCreatedBegin != null){
            queryWrapper.ge(BasePo::getGmtCreated, gmtCreatedBegin);
        }

        Date gmtCreatedEnd = this.getGmtCreatedEnd();
        if (gmtCreatedEnd != null){
            queryWrapper.le(BasePo::getGmtCreated, gmtCreatedEnd);
        }

        Date gmtModifiedBegin = this.getGmtModifiedBegin();
        if (gmtModifiedBegin != null){
            queryWrapper.ge(BasePo::getGmtModified, gmtModifiedBegin);
        }

        Date gmtModifiedEnd = this.getGmtModifiedEnd();
        if (gmtModifiedEnd != null){
            queryWrapper.le(BasePo::getGmtModified, gmtModifiedEnd);
        }

        return queryWrapper;
    }

}
